package org.mch.func.file.createfolder;

import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;

import org.mch.biz.ui.FuncEvent;
import org.mch.biz.ui.FuncPanel;
import org.mch.swingm.core.MFileChooser;

/**
 * 描述：文件操作；
 * 
 * @author MCH；Date：2008-10-01；<BR/>
 * 
 */
public class ClientEvent extends FuncEvent {
	private static final long serialVersionUID = -8590108060646853821L;

	MFileChooser fileChooser;
	List<String> reslist = new ArrayList<String>();

	public ClientEvent(FuncPanel panel) {
		super(panel);
	}

	protected void onButtonClick(ActionEvent e) throws Exception {
		if (e.getSource() == getPanel().getBtn_execute()) {
			onExecute();
		} else if (e.getSource() == getPanel().getBtn_preview()) {
			onPreview();
		}
	}

	protected void onPreview() {
		String text_rule = getPanel().getText_createrule().getValue();

		String textrulelist = getPanel().getText_rulelist().getText();
		String[] rulearray = textrulelist.split("\n");

		for (int i = 0, len = rulearray.length; i < len; i++) {
			String rule = rulearray[i];
			String[] ss1 = rule.split("=");
			String[] ss2 = ss1[1].split(" - ");

			if (ss2[0].compareTo("0") >= 0 && ss2[0].compareTo("9") <= 0) {
				onPreviewReplaceInteger(text_rule, reslist, ss1[0], Integer.parseInt(ss2[0]), Integer.parseInt(ss2[1]));
			} else if (ss2[0].compareTo("a") >= 0 && ss2[0].compareTo("z") <= 0) {
				onPreviewReplaceChar(text_rule, reslist, ss1[0], ss2[0], ss2[1]);
			} else if (ss2[0].compareTo("A") >= 0 && ss2[0].compareTo("Z") <= 0) {
				onPreviewReplaceChar(text_rule, reslist, ss1[0], ss2[0], ss2[1]);
			}
		}

		getPanel().getText_filename().setText(tool.getListLine(reslist));
	}

	private void onPreviewReplaceInteger(String text_rule, List<String> relist, String flag, int begin, int end) {
		if (relist.size() == 0) {
			for (int i = begin; i <= end; i++) {
				relist.add(text_rule.replace(flag, i + ""));
			}
		} else {
			for (int i = 0, len = relist.size(); i < len; i++) {
				String tmp_text_rule = relist.get(i);
				for (int b = begin; b <= end; b++) {
					relist.set(i, tmp_text_rule.replace(flag, b + ""));
				}
			}
		}
	}

	private void onPreviewReplaceChar(String text_rule, List<String> relist, String flag, String begin, String end) {
		char c1 = begin.charAt(0);
		char c2 = end.charAt(0);

		if (relist.size() == 0) {
			char b_c1 = c1;
			do {
				relist.add(text_rule.replace(flag, b_c1 + ""));
				b_c1 = (char) (((int) b_c1) + 1);
			} while ((int) b_c1 <= (int) c2);
		} else {
			for (int i = 0, len = relist.size(); i < len; i++) {
				String tmp_text_rule = relist.get(i);

				char b_c1 = c1;
				do {
					relist.add(tmp_text_rule.replace(flag, b_c1 + ""));
					b_c1 = (char) (((int) b_c1) + 1);
				} while ((int) b_c1 <= (int) c2);
			}
		}
	}

	protected void onExecute() {
		if (reslist == null || reslist.size() > 0) {
			onPreview();
		}

		String topath = getPanel().getText_targetfolder().getValue();

		for (String fname : reslist) {
			File file = new File(topath + File.separator + fname);
			file.mkdir();
		}
	}

	public MFileChooser getFileChooser() {
		if (fileChooser == null) {
			fileChooser = new MFileChooser();
			fileChooser.setPreferredSize(700, 400);
			fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);// 设置能选择文件和文件夹
		}
		return fileChooser;
	}

	public ClientPanel getPanel() {
		return (ClientPanel) super.getPanel();
	}
}